package com.skn;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;

public class TestMail{
	 
    public static void main(String[] args)
    {
    	String user="phhuan504";
                String passwd="Huanpham";
    	new TestMail().receive(user,passwd);
    }
    
  
    public void receive(String username,String password) 

{
        String host = "pop.mail.yahoo.com";
        try {
            Properties prop = new Properties();
            prop.setProperty("mail.pop3.socketFactory.class", 
                                        "javax.net.ssl.SSLSocketFactory");
            prop.setProperty("mail.pop3.socketFactory.fallback", "false");
            prop.setProperty("mail.pop3.port", "995");
            prop.setProperty("mail.pop3.socketFactory.port", "995");
           
            prop.put("mail.pop3.host", host);
            prop.put("mail.store.protocol", "pop3");
            Session session = Session.getDefaultInstance(prop);
            Store store = session.getStore();
            System.out.println("your ID is : "+ username);
            System.out.println("Connecting...");
            store.connect(host, username, password);
            System.out.println("Connected...");
            Folder inbox = store.getDefaultFolder().getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
 
            Message[] msg = inbox.getMessages();
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
 
            for (int i = 0; i < msg.length; i++) {
              System.out.println("Subject:" + msg[i].getSubject());
               System.out.println("Read message? [Y to read / N to end]");
              String line = reader.readLine();
              if ("Y".equalsIgnoreCase(line))
              {
                handleMultipart(msg[i]);
                System.out.println("****************************");
              }
              else if ("N".equalsIgnoreCase(line))
               {
                break;
              }
              else
              {}
            }
            if (inbox != null) {
                inbox.close(true);
            }
            if (store != null) {
                store.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

      private void handle(Message msg) throws Exception {
        System.out.println("From:" + msg.getFrom()[0].toString());
        System.out.println("SentDate:" + msg.getSentDate());
    }
 
    private void handleText(Message msg) throws Exception {
        handle(msg);
        System.out.println("Content:"+msg.getContent());
    }
 
    private void handleMultipart(Message msg) throws Exception {
        String disposition;
        BodyPart part;
        Multipart mp = (Multipart) msg.getContent();
        int mpCount = mp.getCount();
        for (int m = 0; m < mpCount; m++) {
            handle(msg);
            part = mp.getBodyPart(m);
            disposition = part.getDisposition();
            System.out.println(disposition);
            if (disposition != null && disposition.equals(Part.ATTACHMENT))
            {
              BufferedReader areader = new BufferedReader(new InputStreamReader                 (System.in));
              System.out.println("you get an attachment : " + part.getFileName());
              System.out.println("Save attachment? [Y to read / N to end]");
              String line = areader.readLine();
              if ("Y".equalsIgnoreCase(line))
              {
                //saveAttach(part);
              }
              else{}
            } else if(disposition.equals(Part.INLINE)){
                System.out.println(part.getContent());
            } else {}
        }
    }

}